package cn.singno.bob.jpa.base.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import cn.singno.bob.jpa.annotation.core.DynamicSpecification;
import cn.singno.bob.jpa.base.BaseRepository;
import cn.singno.bob.jpa.base.BaseService;

/**
 *<p>名称：BaseService.java</p>
 * <p>描述：业务基础接口</p>
 * <pre>
 * </pre>
 * @author 鲍建明
 * @date 2014年8月11日 下午3:14:54
 * @version 1.0.0
 */
public class BaseServiceImpl<T, ID extends Serializable> implements BaseService<T, ID>
{
	@Autowired
	protected BaseRepository<T, ID> baseRepository;
	

	@Override
	public T findOne(ID id) {
		if(id == null){
			return null;
		}
		return baseRepository.findOne(id);
	}
	

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#save(java.lang.Object)
	 */
	@Override
	public T save(T entity)
	{
		return baseRepository.save(entity);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#updata(java.lang.Object)
	 */
	@Override
	public T updata(T entity)
	{
		return baseRepository.update(entity);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(ID id)
	{
		baseRepository.delete(id);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findAll()
	 */
	@Override
	public List<T> findAll()
	{
		return baseRepository.findAll();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(int, int)
	 */
	@Override
	public List<T> findList(int pageNow, int pageSize)
	{
		Page<T> result = this.findPageList(builderPageable(pageNow, pageSize));
		return result.getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(org.springframework.data.domain.Pageable)
	 */
	@Override
	public List<T> findList(Pageable pageable)
	{
		return this.findPageList(pageable).getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(java.lang.Object, org.springframework.data.domain.Pageable)
	 */
	@Override
	public List<T> findList(T searchEntity, Pageable pageable)
	{
		Page<T> result = baseRepository.findAll(DynamicSpecification.toPredicate(searchEntity), pageable);
		return result.getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(java.lang.Object, int, int)
	 */
	@Override
	public List<T> findList(T searchEntity, int pageNow, int pageSize)
	{
		Page<T> result = baseRepository.findAll(DynamicSpecification.toPredicate(searchEntity), builderPageable(pageNow, pageSize));
		return result.getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(int, int)
	 */
	@Override
	public Page<T> findPageList(int pageNow, int pageSize)
	{
		return baseRepository.findAll(builderPageable(pageNow, pageSize));
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<T> findPageList(Pageable pageable)
	{
		return baseRepository.findAll(pageable);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(java.lang.Object, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<T> findPageList(T searchEntity, Pageable pageable)
	{
		return baseRepository.findAll(DynamicSpecification.toPredicate(searchEntity), pageable);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(java.lang.Object, int, int)
	 */
	@Override
	public Page<T> findPageList(T searchEntity, int pageNow, int pageSize)
	{
		return baseRepository.findAll(DynamicSpecification.toPredicate(searchEntity), builderPageable(pageNow, pageSize));
	}

	

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#save(java.lang.Iterable)
	 */
	@Override
	public void save(Iterable<T> entities)
	{
		baseRepository.save(entities);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#delete(java.lang.Iterable)
	 */
	@Override
	public void delete(Iterable<T> entities)
	{
		baseRepository.delete(entities);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#count(java.lang.Object)
	 */
	@Override
	public long count(T searchEntity)
	{
		return baseRepository.count(DynamicSpecification.toPredicate(searchEntity));
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#count()
	 */
	@Override
	public long count()
	{
		return baseRepository.count();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findOne(java.lang.Object)
	 */
	@Override
	public T findOne(T searchEntity)
	{
		return baseRepository.findOne(DynamicSpecification.toPredicate(searchEntity));
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findAll(org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findAll(Sort sort)
	{
		return baseRepository.findAll(sort);
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(int, int, org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findList(int pageNow, int pageSize, Sort sort)
	{
		return this.findPageList(builderPageable(pageNow, pageSize, sort)).getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findList(java.lang.Object, int, int, org.springframework.data.domain.Sort)
	 */
	@Override
	public List<T> findList(T searchEntity, int pageNow, int pageSize, Sort sort)
	{
		return this.findPageList(searchEntity, builderPageable(pageNow, pageSize, sort)).getContent();
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(int, int, org.springframework.data.domain.Sort)
	 */
	@Override
	public Page<T> findPageList(int pageNow, int pageSize, Sort sort)
	{
		return findPageList(builderPageable(pageNow, pageSize, sort));
	}

	/* (non-Javadoc)
	 * @see cn.singno.bob.jpa.base.BaseService#findPageList(java.lang.Object, int, int, org.springframework.data.domain.Sort)
	 */
	@Override
	public Page<T> findPageList(T searchEntity, int pageNow, int pageSize,
			Sort sort)
	{
		return this.findPageList(searchEntity, builderPageable(pageNow, pageSize, sort));
	}
	
	/************************************* private *************************************************/
	//build page
	private Pageable builderPageable(int pageNow, int pageSize){
		return this.builderPageable(pageNow, pageSize, (Sort)null);
	}
	//build page
	private Pageable builderPageable(int pageNow, int pageSize, Sort sort){
		Pageable page = new PageRequest(pageNow, pageSize, sort);
		return page;
	}





}
